package engine.newUi
{
	import engine.control.ControlCreater;
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.RoleVo;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.KungfuItem;
	
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.ColorMatrixFilter;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;
	import newx.utils.FilterUtil;

	/**
	 *功法兑现 
	 * @author yanghr
	 * 
	 */	
	public class ExChangePowerLogic
	{
		private static var _self:ExChangePowerLogic;
		
		private const CONFIGID:String="ConfigID";
		private const ITEM:String="item";
		private const TXT:String="txt_0";
		private const DES:String="txt_1";
		private const BTN_LAB:String="txt_2";
		private const BTN:String="btn";
		
		private const GRAY:ColorMatrixFilter=FilterUtil.GrayColorFilter;
		private const LAB:String="兑换";
		private var _ui:ExChangePowerUI;
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		private var _bindReminCountFlag:int=-1;
		private var _leaveCount:int;
		
		private var _configArr:Array;
		private var _exchangeObj:Object;
		public static function getInstance():ExChangePowerLogic
		{
			if(_self==null)
			{
				_self=new ExChangePowerLogic();
			}
			return _self;
		}
		public function get ui():ExChangePowerUI
		{
			if(_ui==null)
			{
				_ui=new ExChangePowerUI;
			}
			return _ui;
		}
		public function ExChangePowerLogic()
		{
		}
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_ui.scroller.addView(_ui.mainGroup);
			_ui.mainGroup.x=0;
			_ui.mainGroup.y=0;
//			_ui.scroller.vscroll = 0;
			_configArr=String(StaticIni.getIniObj("KungfuExc")["ID"]).split(",");
			_exchangeObj=StaticIni.getIniObj("RedeemKungFu");
			
		}
		/**
		 *刷新伙伴列表 
		 * 
		 */		
		private function refreshList(view:Object=null):Boolean
		{
			var b:Boolean=true;
			var $this:Object=this;
			//重置 好友列表
			if(_configArr.length	!=	ui.mainGroup.row)
			{
				var skin:String=ui.mainGroup.viewSkin;
				//				var col:int=ui.actorGroup.col;
				var row:int=_configArr.length;
				var x:Number=ui.mainGroup.x;
				var y:Number=ui.mainGroup.y;
				var pa:DisplayObjectContainer=ui.mainGroup.parent;
				
				//				ui.actorGroup
//				removeAllMcBlind();
				ui.mainGroup.removeEventListener(MouseEvent.CLICK,actorClickHandle,true);
//				ui.mainGroup.removeItemsEvent(MouseEvent.ROLL_OVER,grouprollOver)
//				ui.mainGroup.removeItemsEvent(MouseEvent.ROLL_OUT,grouprollOut)
				ui.mainGroup.parent.removeChild(ui.mainGroup)
				ui.mainGroup=null;
				//创建
				ui.mainGroup=ControlCreater.getInstance().CreateNxViewGroup(row,1,skin);
				ui.mainGroup.addEventListener(MouseEvent.CLICK,actorClickHandle,true);
//				ui.mainGroup.addItemsEvent(MouseEvent.ROLL_OVER,grouprollOver)
//				ui.mainGroup.addItemsEvent(MouseEvent.ROLL_OUT,grouprollOut)
				ui.mainGroup.x=x;
				ui.mainGroup.y=y;
				pa.addChild(ui.mainGroup);
//				initPartnerList();
				//重新初始化列表
				//				refreshList(_role);
				b= false;
				_ui.scroller.addView(_ui.mainGroup);
			}
			for (var i:int = 0; i < _configArr.length; i++) 
			{
				view=_tip.getStaticObjWithID(_configArr[i]);
				setMc(view);
			}
//			currentSelectedM=ui.actorGroup.getItemByIndex(Math.min(selectIndex,_roleArr.length-1));
			//			setMc(view)
			//				ui.actorGroup.row
			return b;
			function setMc(viewObj:Object):void
			{
				if(!viewObj)return ;
				var indexxx:int=uint(_configArr.indexOf(view[CONFIGID]));
				var mc:MovieClip	=	ui.mainGroup.getItemByIndex(indexxx);
				var tipstr:String;
				var vars:int;
				if(mc== null)return;
				var item:KungfuItem=new KungfuItem(null,viewObj); 
				mc[TXT].htmlText=item.getColorName();
				mc[DES].htmlText=item.describle;
				var configNeed:Array=	String(_exchangeObj[viewObj[CONFIGID]]).split(",");
				var count:int=configNeed[0];
				var needLevel:int=configNeed[1];
				var nowCount:int=$this._leaveCount
				if(count)
				{
					var b:Boolean=needLevel>_role.getPlayerLevel();
					if(b	||	count>nowCount)
					{
						(mc[BTN] as SimpleButton).enabled=false;
						mc[BTN].filters=[GRAY];
						tipstr=b?"Kungfu_ExcTips1":"Kungfu_ExcTips2";
						vars=b?needLevel:count;
					}
					else
					{
						(mc[BTN] as SimpleButton).enabled=true;
						mc[BTN].filters=null;
						tipstr="Kungfu_ExcTips2";
						vars=count
					}
					
				}
				mc[BTN].visible=mc[BTN_LAB].visible=Boolean(count);
				_tip.objRollshowTips(mc[BTN],function():String{
					return StaticIni.getSwfLangStrVar(tipstr,[vars]);
				})
				mc[BTN_LAB].htmlText=LAB;
				mc[BTN_LAB].mouseEnabled=false;
				mc[CONFIGID]=viewObj[CONFIGID];
				
			}
		}
		public function	BeforeShow():void
		{
			addAllEventListener()
			refreshList();
			_ui.scroller.vscroll=0;
		}
		public function	AfterHide():void
		{
			removeAllEventListener()
		}
		public function set remainCount(value:int):void
		{
//			if(_leaveCount==value)
//				return 
			
			_leaveCount=value;
			_ui.countLab.htmlStr=StaticIni.getSwfLangStrVar("Kungfu_UIString5",[value])//"残卷数量："+value;
			refreshList();
		}
		public function addAllEventListener():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			ui.mainGroup.addEventListener(MouseEvent.CLICK,actorClickHandle,true);
			if(_bindReminCountFlag==-1)
			{
				_bindReminCountFlag= _bind.BindProp(_role,"DefaultCount",this,"remainCount")
			}
		}
		public function removeAllEventListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			ui.mainGroup.removeEventListener(MouseEvent.CLICK,actorClickHandle,true);
			if(_bindReminCountFlag != -1)
			{
				_bindReminCountFlag	 = _bind.UnbindProp(_role,"DefaultCount",_bindReminCountFlag)
			}
		}
		private function actorClickHandle(e:Event):void
		{
//			trace("ExChangePowerLogic.actorClickHandle(e)",e.target);
			if(e.target is SimpleButton	&& e.target.enabled)
			{
				var configid:int=e.target.parent[CONFIGID];
//				trace("ExChangePowerLogic.actorClickHandle(e)configid",configid);
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REDEEM_KUNGFU,configid);
				
			}
		}
		private function close(e:Event):void
		{
			_ui.hide();
		}
	}
}